/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "latex.h"
#include "mdown.h"
#include "global.h"

#include <QFile>
#include <QString>
#include <QTextStream>
#include <QSettings>

QString Latex::partToTex(Part* part)
{
    QString doc = "\\section{"+ part->getTitle() + "}\n";
    doc += Mdown::toLatex(part->getContents());
    return doc;
}

QString Latex::miniToTex(MiniTutorial* mt)
{
    QString doc = "\\section*{" + QObject::tr("Introdution", "Latex Section Introduction");
    doc += "}\n";
    doc += Mdown::toLatex(mt->getIntro()->getContents());

    for(QList<Part*>::const_iterator it = mt->allParts().begin(); it != mt->allParts().end(); ++it)
	doc += partToTex(*it);
    
    doc += "\\section*{"+ QObject::tr("Conclusion", "Latex Section Conclusion");
    doc += "}\n";
    doc += Mdown::toLatex(mt->getConclu()->getContents());
    return doc;
}

QString Latex::bigToTex(BigTutorial* bt)
{
    QString doc = "\\section*{" + QObject::tr("Introdution", "Latex Section Introduction");
    doc += "}\n";
    doc += Mdown::toLatex(bt->getIntro()->getContents());
    for(QList<BigTutorialPart*>::const_iterator it = bt->allParts().begin(); 
	it != bt->allParts().end(); ++it)
    {
	doc += "\\chapter{"+ (*it)->getTitle()+"}\n";
	doc += Mdown::toLatex((*it)->getIntro()->getContents())+"\n";
	for(QList<MiniTutorial*>::const_iterator it2 = (*it)->allChapters().begin();
	    it2 != (*it)->allChapters().end(); ++it2)	
	    doc += miniToTex(*it2);
	doc += Mdown::toLatex((*it)->getConclu()->getContents())+"\n";
    }
    
    doc += "\\section*{"+ QObject::tr("Conclusion", "Latex Section Conclusion");
    doc += "}\n";
    doc += Mdown::toLatex(bt->getConclu()->getContents());
    return doc;
	
}
	
// Exports File Methods
	
void Latex::partToFile(Part* part, const QString& fname)
{
    QString header;
    
    QSettings settings("VTO", "Vos Tutos Offline");
    QString author = settings.value("latexconfig/author").toString();
    QString documentclassopt = settings.value("latexconfig/fontsize").toString() + "," + settings.value("latexconfig/paperformat").toString();
    QString encoding = settings.value("latexconfig/encodingtype").toString();
    bool showdate = settings.value("latexconfig/showdate").toBool();

    bool customHeader = settings.value("latexconfig/customheader").toBool();

    if(customHeader)
    {
	header = settings.value("latexconfig/header").toString();
	header.replace(QString("%title"), part->getTitle());
	header.replace(QString("%author"), author);
    }
    else
    {
	
	header = "\\documentclass["+ documentclassopt +"]{article}\n";
	header += "\\usepackage["+encoding+"]{inputenc}\n";
	header += "\\begin{document}\n";
	if(!showdate)
	    header += "\\date{\\today}\n";
	header += "\\title{"+ part->getTitle() + "}\n";
	header += "\\author{"+ author +"}\n";
	header += "\\maketitle\n";
    }

    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
	QTextStream ts( &file );
	ts << header + partToTex(part);
	ts << "\n\\end{document}\n";
	ts << QObject::tr("\\\\This document is available under the terms of the ");
	ts << VTO::licence(part->getLicence()) + "\n";

	file.close();
    }
    else
	throw 401;
}

void Latex::miniToFile(MiniTutorial* mt, const QString& fname)
{
    QString header;
    
    QSettings settings("VTO", "Vos Tutos Offline");
    QString author = settings.value("latexconfig/author").toString(); 
    QString documentclassopt = settings.value("latexconfig/fontsize").toString() + "," + settings.value("latexconfig/paperformat").toString();
    QString encoding = settings.value("latexconfig/encodingtype").toString();
    bool showdate = settings.value("latexconfig/showdate").toBool();

    bool customHeader = settings.value("latexconfig/customheader").toBool();

    if(customHeader)
    {
	header = settings.value("latexconfig/header").toString();
	header.replace(QString("%title"), mt->getTitle());
	header.replace(QString("%author"), author);
    }
    else
    {
	
	header = "\\documentclass["+ documentclassopt +"]{article}\n";
	header += "\\usepackage["+encoding+"]{inputenc}\n";
	header += "\\begin{document}\n";
	if(!showdate)
	    header += "\\date{\\today}\n";
	header += "\\title{"+ mt->getTitle() + "}\n";
	header += "\\author{"+ author +"}\n";
	header += "\\maketitle\n";
    }
    
    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
    
    	QTextStream ts( &file );
    	ts << header + miniToTex(mt);
	ts << "\n\\end{document}\n";
	ts << QObject::tr("\\\\This document is available under the terms of the ");
	ts << VTO::licence(mt->getLicence()) + "\n";

    	file.close();
    }
    else
    	throw 401;
}

void Latex::bigToFile(BigTutorial* bt, const QString& fname)
{
    QString header;
    
    QSettings settings("VTO", "Vos Tutos Offline");
    QString author = settings.value("latexconfig/author").toString(); 
    QString documentclassopt = settings.value("latexconfig/fontsize").toString() + "," + settings.value("latexconfig/paperformat").toString();
    QString encoding = settings.value("latexconfig/encodingtype").toString();
    bool showdate = settings.value("latexconfig/showdate").toBool();

    bool customHeader = settings.value("latexconfig/customheader").toBool();

    if(customHeader)
    {
	header = settings.value("latexconfig/header").toString();
	header.replace(QString("%title"), bt->getTitle());
	header.replace(QString("%author"), author);
    }
    else
    {
	
	header = "\\documentclass["+ documentclassopt +"]{report}\n";
	header += "\\usepackage["+encoding+"]{inputenc}\n";
	header += "\\begin{document}\n";
	if(!showdate)
	    header += "\\date{\\today}\n";
	header += "\\title{"+ bt->getTitle() + "}\n";
	header += "\\author{"+ author +"}\n";
	header += "\\maketitle\n";
    }
    
    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
    
    	QTextStream ts( &file );
    	ts << header + bigToTex(bt);
	ts << "\n\\end{document}\n";
	ts << QObject::tr("\\\\This document is available under the terms of the ");
	ts << VTO::licence(bt->getLicence()) + "\n";

    	file.close();
    }
    else
    	throw 401;
}
	

